package com.block.test.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.block.common.bean.SessionBean;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.exception.NotifyExceptionService;
import com.block.common.model.vo.ResultInfoVO;
import com.block.common.service.IAddressInfoService;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.AddressInfo;
import com.block.test.service.IValidateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SourceCheckUserValidateService extends IValidateService {

    @Autowired
    IAddressInfoService addressInfoService;
    @Autowired
    NotifyExceptionService notifyExceptionService;

    @Override
    public ResponseVo validate(Object object, JSONObject jsonObject, SessionBean sessionBean) {
        return null;
    }

    @Override
    public ResultInfoVO validate2(Object object, JSONObject jsonObject) {

        String cityCode = jsonObject.getString("cityCode");
        String city = jsonObject.getString("city");
        if (StrUtil.isEmpty(cityCode)){
            return ResultInfoVO.error(AppRspCodeConstant.PARAM_ERROR, "cityCode不能为空");
        }
        AddressInfo addressInfo = addressInfoService.getAddressInfoByCityCode(cityCode);
        if (ObjectUtil.isEmpty(addressInfo)){
            String message = StrUtil.format("【city】{}【cityCode】{}", city, cityCode);
//            notifyExceptionService.notifyNoticeInfo("SOURCE_CHECK_PARAM_CITY_ERROR", "线索撞库城市编码不支持", message, null);
            return ResultInfoVO.error(AppRspCodeConstant.PARAM_ERROR, StrUtil.format("cityCode[{}]暂不支持该地区", cityCode));
        }


        String mobileMd5 = jsonObject.getString("mobileMd5");
        if (StrUtil.isEmpty(mobileMd5)){
            return ResultInfoVO.error(AppRspCodeConstant.PARAM_ERROR, "mobileMd5不能为空");
        }

        String realName = jsonObject.getString("realName");
        if (StrUtil.isEmpty(realName)){
            return ResultInfoVO.error(AppRspCodeConstant.PARAM_ERROR, "realName不能为空");
        }

        Integer age = jsonObject.getInteger("age");
        if (ObjectUtil.isEmpty(age)){
            return ResultInfoVO.error(AppRspCodeConstant.PARAM_ERROR, "age不能为空");
        }
        if (age < 23 || age > 55){
            return ResultInfoVO.error(AppRspCodeConstant.PARAM_ERROR, "年龄范围23-55");
        }

        return null;
    }
}
